package org.knot.mockejb3.interceptor;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.*;

public class InterceptableProxy implements MethodInterceptor {
    
    private Class ifaceClass;
    private Object implObj;

    private InterceptorInvoker interceptorInvoker = new InterceptorInvoker();
    
    public static Object create( Class ifaceClass, Object implObj ){
        Enhancer e = new Enhancer();
        e.setSuperclass( ifaceClass );
       
        e.setCallback( new InterceptableProxy( ifaceClass, implObj ) );
        
        return e.create();
        
    }

    InterceptableProxy( Class ifaceClass, Object implObj ){
        this.ifaceClass = ifaceClass;
        this.implObj = implObj;
    }
        
    public Object intercept(Object obj, Method proxyMethod, Object[] paramVals,
            MethodProxy proxy) throws Throwable {
        
        Method implMethod = implObj.getClass().getMethod( proxyMethod.getName(), 
                proxyMethod.getParameterTypes() );
        
        return  interceptorInvoker.invoke( obj, proxyMethod, implObj, implMethod, paramVals );
        
    }
    

}
